package com.castle.fortress.admin.member.service.impl;

import cn.hutool.core.util.StrUtil;

import com.castle.fortress.admin.member.mapper.MemberCommentMapper;
import com.castle.fortress.common.utils.ConvertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.castle.fortress.admin.member.entity.MemberThumbsUpEntity;
import com.castle.fortress.admin.member.dto.MemberThumbsUpDto;
import com.castle.fortress.admin.member.mapper.MemberThumbsUpMapper;
import com.castle.fortress.admin.member.service.MemberThumbsUpService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.util.List;

/**
 * 会员点赞表 服务实现类
 *
 * @author castle
 * @since 2023-02-20
 */
@Service
public class MemberThumbsUpServiceImpl extends ServiceImpl<MemberThumbsUpMapper, MemberThumbsUpEntity> implements MemberThumbsUpService {
    @Autowired
    private MemberCommentMapper memberCommentMapper;
    /**
    * 获取查询条件
    * @param memberThumbsUpDto
    * @return
    */
    public QueryWrapper<MemberThumbsUpEntity> getWrapper(MemberThumbsUpDto memberThumbsUpDto){
        QueryWrapper<MemberThumbsUpEntity> wrapper= new QueryWrapper();
        if(memberThumbsUpDto != null){
            MemberThumbsUpEntity memberThumbsUpEntity = ConvertUtil.transformObj(memberThumbsUpDto,MemberThumbsUpEntity.class);
            wrapper.eq(memberThumbsUpEntity.getId() != null,"id",memberThumbsUpEntity.getId());
            wrapper.eq(StrUtil.isNotEmpty(memberThumbsUpEntity.getType()),"type",memberThumbsUpEntity.getType());
            wrapper.eq(memberThumbsUpEntity.getMemberId() != null,"member_id",memberThumbsUpEntity.getMemberId());
            wrapper.eq(memberThumbsUpEntity.getTargetId() != null,"target_id",memberThumbsUpEntity.getTargetId());
            wrapper.like(memberThumbsUpEntity.getCreateTime() != null,"create_time",memberThumbsUpEntity.getCreateTime());
        }
        return wrapper;
    }


    @Override
    public IPage<MemberThumbsUpDto> pageMemberThumbsUp(Page<MemberThumbsUpDto> page, MemberThumbsUpDto memberThumbsUpDto) {
        QueryWrapper<MemberThumbsUpEntity> wrapper = getWrapper(memberThumbsUpDto);
        Page<MemberThumbsUpEntity> pageEntity = new Page(page.getCurrent(), page.getSize());
        Page<MemberThumbsUpEntity> memberThumbsUpPage=baseMapper.selectPage(pageEntity,wrapper);
        Page<MemberThumbsUpDto> pageDto = new Page(memberThumbsUpPage.getCurrent(), memberThumbsUpPage.getSize(),memberThumbsUpPage.getTotal());
        pageDto.setRecords(ConvertUtil.transformObjList(memberThumbsUpPage.getRecords(),MemberThumbsUpDto.class));
        return pageDto;
    }


    @Override
    public List<MemberThumbsUpDto> listMemberThumbsUp(MemberThumbsUpDto memberThumbsUpDto){
        QueryWrapper<MemberThumbsUpEntity> wrapper = getWrapper(memberThumbsUpDto);
        List<MemberThumbsUpEntity> list = baseMapper.selectList(wrapper);
        return ConvertUtil.transformObjList(list,MemberThumbsUpDto.class);
    }

    @Override
    public boolean saveMemberThumbsUp(MemberThumbsUpEntity memberThumbsUpEntity) {
        if(baseMapper.insert(memberThumbsUpEntity)>0){
            //评论点赞
            if("00".equals(memberThumbsUpEntity.getType())){
                memberCommentMapper.addThumbsUp(memberThumbsUpEntity.getTargetId());
            }
            return true;
        }else{
            return false;
        }
    }

    @Override
    public boolean cancelMemberThumbsUp(MemberThumbsUpDto memberThumbsUpDto) {
        if(baseMapper.deleteById(memberThumbsUpDto.getId())>0){
            //评论点赞
            if("00".equals(memberThumbsUpDto.getType())){
                memberCommentMapper.cancelThumbsUp(memberThumbsUpDto.getTargetId());
            }
            return true;
        }
        return false;
    }
}

